<?php

/**
 * 前台 首页 模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: index.class.php 145 2013-01-22 17:54:45Z zwt007 $
 * http://www.t-mac.org；
 */
class index extends Model
{

    private $db;
    private $article_table;    
    private $category_model;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->article_table = DB_PREFIX . 'article';        
        $this->category_model = Tmac::model('category');
    }

    /**
     * 取全站公共部分
     * @return type 
     */
    public function getCommon()
    {
        //取config配置文件
        $configcache = Tmac::config('configcache.config', APP_NAME, '.inc.php');
        $configcache['cfg_powerby'] = $this->HD($configcache['cfg_powerby']);
        return $configcache;
    }

    /**
     * 获取所有资讯
     * return article_class,pages
     */
    public function getArticleList($cat_id = null, $url = null, $search_keyword=null)
    {
        $indexurl = Tmac::config('configcache.config.cfg_indexurl', APP_NAME, '.inc.php');
        $separator = '&lt;--more--&gt;';
        $where = " WHERE status='publish'";
        if ($url == '')
            $url = PHP_SELF . '?m=article';
        if (!empty($cat_id)) {
            $cat_ids = Tmac::getCache('cat_ids', array($this->category_model, 'getSonTreeList'), array($cat_id), 86400);
            $where .= " AND cat_id IN($cat_ids)";
            $url .= "&cat_id=$cat_id";
        }

        if ($search_keyword != '') {
            $where .= " AND title LIKE '%" . $search_keyword . "%'";
            $url .= "&search_keyword=$search_keyword";
        }

        //$url .= '&page=';
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->article_table {$where}");
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(2);
        $limit = $pages->getSqlLimit();
        $sql = "SELECT article_id, cat_id, title,fuck, time, click_count, comment_count, content_descrption,tag_info FROM {$this->article_table}{$where} ORDER BY article_id DESC LIMIT $limit";
        $query = $this->db->query($sql);

        //取所有的资讯栏目 不用LEFT JOIN取class_name
        $rs_class = $this->category_model->getClass();

        $ary_class = array();

        //重组栏目category信息数组
        foreach ($rs_class AS $kk => $vv) {
            $ary_class['cat_name'][$vv['cat_id']] = $vv['cat_name'];
            $ary_class['category_nicename'][$vv['cat_id']] = $vv['category_nicename'];
        }

        $result_array = array();
        //遍历通过class_id取class_name
        if ($query) {
            while ($rs = mysql_fetch_assoc($query)) {
                $rs['cat_name'] = $ary_class['cat_name'][$rs['cat_id']];
                $rs['category_nicename'] = $ary_class['category_nicename'][$rs['cat_id']];
                $rs['time'] = date('Y-m-d', $rs['time']);
                $rs['tag_info'] = json_decode($rs['tag_info'], true);
                //判断有没有more的内容放到content_description
                if (strpos($rs['content_descrption'], $separator)) {
                    $replace = '<p><a class="more-link" href="' . $indexurl . $rs['category_nicename'] . '/' . $rs['article_id'] . '#more-' . $rs['article_id'] . '">继续阅读 <span class="meta-nav">→</span></a></p>';
                    $rs['content_descrption'] = str_replace($separator, $replace, $rs['content_descrption']);
                }
                $result_array[] = $rs;
            }
            unset($rs);
        }

        //把文章的当前page写到cookies里
        HttpResponse::setCookie('article_page', $pages->getNowPage());

        $ErrorMsg = '';
        if ($count == 0) {
            $ErrorMsg = "暂无资讯文章!";
        }

        $result = array('rs' => $result_array, 'pageCurrent' => $pages->getNowPage(), 'page' => $pages->show(), 'ErrorMsg' => $ErrorMsg);
        return $result;
    }

}